package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategroyMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.CategroyService;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategroyServicelmpl implements CategroyService {


    @Autowired
    private CategroyMapper categroyMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增分类
     *
     * @param categoryDTO
     */
    @Override
    public void save(CategoryDTO categoryDTO) {

        Category category = new Category();
        //对象属性拷贝
        BeanUtils.copyProperties(categoryDTO, category);
        //通过程序填充其他属性
        category.setStatus(StatusConstant.DISABLE);
       /* category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());*/
        //插入数据
        categroyMapper.insert(category);
    }

    /**
     * 分类分页查询
     *
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        //基于PageHelper实现分页
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        //分页查询
        Page<Category> page = categroyMapper.pageQuery(categoryPageQueryDTO);
        //封装返回结果
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 删除分类
     *
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        //根据分类id查询菜品
        Integer count = dishMapper.countByCategoryId(id);
        if (count > 0) {
            //判断当前分类是否关联了菜品，如果关联了则不能删除，抛出业务异常
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
      //  count = setmealMapper.countByCategoryId(id);
        if (count > 0) {
            //判断当前分类是否关联了菜品，如果关联了则不能删除，抛出业务异常
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        categroyMapper.deleteById(id);

    }

    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        //设置修改时间、修改人
        /*category.setUpdateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());*/
        categroyMapper.update(category);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        //构造对象
        Category category = Category.builder()
                .id(id)
                .status(status)
               /* .updateTime(LocalDateTime.now())
                .updateUser(BaseContext.getCurrentId())*/
                .build();

        //执行数据库修改操作
        categroyMapper.update(category);
    }

    /**
     * 根据类型查询分类
     *
     * @param
     * @return
     */
    @Override
    public List<Category> list(Category category) {
        return categroyMapper.list(category);
    }
}
